﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Dycox.ComponentModel;
using Newtonsoft.Json;
using System.ComponentModel;

namespace Dycox.Workflows
{
    [DataContract]
    internal class ThreadProperties : NotifyPropertyChanged
    {
        public ThreadProperties()
        {

        }

        private string _lockName;
        [DataMember(Name = "Lock", EmitDefaultValue = false), DefaultValue(null)]
        public string LockName
        {
            get { return _lockName; }
            set
            {
                if (_lockName != value)
                {
                    _lockName = value;
                    SendPropertyChanged("LockName");
                }
            }
        }

        private int _lockingStep;
        [DataMember(Name = "LockingStep", EmitDefaultValue = false), DefaultValue(0)]
        public int LockingStep
        {
            get { return _lockingStep; }
            set
            {
                if (_lockingStep != value)
                {
                    _lockingStep = value;
                    SendPropertyChanged("LockingStep");
                }
            }
        }

        private bool _isLocked;
        [DataMember(Name = "IsLocked", EmitDefaultValue = false), DefaultValue(false)]
        public bool IsLocked
        {
            get { return _isLocked; }
            set
            {
                if (_isLocked != value)
                {
                    _isLocked = value;
                    SendPropertyChanged("IsLocked");
                }
            }
        }

        private WorkflowComponentStatus _suspendingStatus;
        [DataMember(EmitDefaultValue = false), DefaultValue(typeof(WorkflowComponentStatus), "Idling")]
        public WorkflowComponentStatus SuspendingStatus
        {
            get { return _suspendingStatus; }
            set
            {
                if (_suspendingStatus != value)
                {
                    _suspendingStatus = value;
                    SendPropertyChanged("SuspendingStatus");
                }
            }
        }

        private ComponentIdentifier _suspendingStep;
        [DataMember(EmitDefaultValue = false), DefaultValue(typeof(ComponentIdentifier), "Empty")]
        public ComponentIdentifier SuspendingStep
        {
            get { return _suspendingStep; }
            set
            {
                if (_suspendingStep != value)
                {
                    _suspendingStep = value;
                    SendPropertyChanged("SuspendingStep");
                }
            }
        }


        //private string _appData;
        //[DataMember(EmitDefaultValue = false), DefaultValue(null)]
        //public string AppData
        //{
        //    get { return _appData; }
        //    set
        //    {
        //        if (_appData != value)
        //        {
        //            _appData = value;
        //            SendPropertyChanged("AppData");
        //        }
        //    }
        //}


        public bool IsSynchronizing
        {
            get { return _lockName != null; }
        }
	

        public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }

        public static ThreadProperties Parse(string value)
        {
            if (value == null)
                return null;

            if (value.Length == 0)
                return new ThreadProperties();

            return JsonConvert.DeserializeObject<ThreadProperties>(value);
        }
    }
}
